//347.前 K个高频元素
//https://leetcode.cn/problems/top-k-frequent-elements/
class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int,int> hash;
        for(int n:nums) ++hash[n];
        vector<int> ret;
        ret.reserve(k);
        priority_queue<pair<int,int>> pq;
        for(auto& [k,v]:hash) pq.push({v,k});
        for(int i = 0;i<k && !pq.empty();++i)
        {
            ret.push_back(pq.top().second);
            pq.pop();
        }
        return ret;
    }
};